//
//  UIHelper.m
//  CloudAlbum
//
//  Created by WisdomsLab on 13-11-11.
//  Copyright (c) 2013年 Huanhao Tech. All rights reserved.
//

#import "UIHelper.h"
#import <MBProgressHUD.h>

@implementation UIHelper

SINGLETON_FOR_CLASS(UIHelper);

+(void)showHudWithDuration:(NSString *)message{
    main_async(^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:MAIN_WINDOW animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = message;
    });
    double delayInSeconds = 1.6;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [MBProgressHUD hideAllHUDsForView:MAIN_WINDOW animated:YES];
    });
}

+(void)showHudWithDuration:(NSString *)message :(double)delay{
    main_async(^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:MAIN_WINDOW animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = message;
    });
    double delayInSeconds = delay;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [MBProgressHUD hideAllHUDsForView:MAIN_WINDOW animated:YES];
    });
}


+(void)showHud:(NSString *)message{
    main_async(^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:MAIN_WINDOW animated:YES];
        hud.labelText = message;
    });
}

+(void)hideHud{
    main_async(^{
        [MBProgressHUD hideAllHUDsForView:MAIN_WINDOW animated:YES];
    });
}

-(void)showErrorHud:(NSString *)message{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:MAIN_WINDOW animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 44)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 44)];
    [imageView setImage:[[UIImage imageNamed:@"hud_error_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5) resizingMode:UIImageResizingModeStretch]];
    [cView addSubview:imageView];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(11, 7, 214, 30)];
    label.textColor = UIColorFromHex(0Xc04a48);
    label.backgroundColor = [UIColor clearColor];
    label.text = message;
    [label sizeToFit];
    [cView addSubview:label];
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(236, 0, 44, 44)];
    [btn setImage:[UIImage imageNamed:@"hud_error_close"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    [cView addSubview:btn];
    hud.customView = cView;
}

-(void)showWarningHud:(NSString *)message{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:MAIN_WINDOW animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 44)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 44)];
    [imageView setImage:[[UIImage imageNamed:@"hud_error_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5) resizingMode:UIImageResizingModeStretch]];
    [cView addSubview:imageView];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(11, 7, 214, 30)];
    label.textColor = UIColorFromHex(0Xc09853);
    label.text = message;
    [label sizeToFit];
    [cView addSubview:label];
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(236, 0, 44, 44)];
    [btn setImage:[UIImage imageNamed:@"hud_error_close"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    [cView addSubview:btn];
    hud.customView = cView;
}

-(void)showInfoHud:(NSString *)message{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:MAIN_WINDOW animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    
    UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 44)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 44)];
    [imageView setImage:[[UIImage imageNamed:@"hud_error_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5) resizingMode:UIImageResizingModeStretch]];
    [cView addSubview:imageView];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(11, 7, 214, 30)];
    label.textColor = UIColorFromHex(0X3a95cc);
    label.text = message;
    [label sizeToFit];
    [cView addSubview:label];
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(236, 0, 44, 44)];
    [btn setImage:[UIImage imageNamed:@"hud_error_close"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    [cView addSubview:btn];
    hud.customView = cView;
}

-(void)showSuccessHud:(NSString *)message{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:MAIN_WINDOW animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    
    UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 44)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 44)];
    [imageView setImage:[[UIImage imageNamed:@"hud_error_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5) resizingMode:UIImageResizingModeStretch]];
    [cView addSubview:imageView];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(11, 7, 214, 30)];
    label.textColor = UIColorFromHex(0X46885b);
    label.text = message;
    [label sizeToFit];
    [cView addSubview:label];
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(236, 0, 44, 44)];
    [btn setImage:[UIImage imageNamed:@"hud_error_close"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    [cView addSubview:btn];
    hud.customView = cView;
}

-(void)closeAction:(id)sender{
    NSLog(@"closeAction");
    main_async(^{
        [MBProgressHUD hideAllHUDsForView:MAIN_WINDOW animated:YES];
    });
}
@end
